package com.ruoyi.platform.system.menu.manager;

import com.ruoyi.platform.system.menu.enums.MenuEnum;
import com.ruoyi.platform.system.menu.pojo.MenuTreeDTO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单权限表
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-16 02:15:03
 */
public class MenuManager {
    /**
     * 获取菜单列表
     */
    public static List<MenuTreeDTO> getMenuList() {
        List<MenuTreeDTO> tree = Arrays.stream(MenuEnum.values()).filter(x -> Objects.equals(x.getParentCode(), "0"))
                .map(MenuTreeDTO::new).collect(Collectors.toList());

        List<MenuEnum> allMenuList = Arrays.stream(MenuEnum.values()).collect(Collectors.toList());
        getMenuTree(tree, allMenuList);
        return tree;
    }

    /**
     * 获取菜单列表
     */
    public static List<MenuTreeDTO> getMenuList(List<MenuEnum> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return getMenuList();
        }

        return getIsPermission(menuList);
    }

    /**
     * 获取用户是否有权限
     */
    private static List<MenuTreeDTO> getIsPermission(List<MenuEnum> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return getMenuList();
        }

        List<MenuTreeDTO> tree = Arrays.stream(MenuEnum.values()).filter(x -> Objects.equals(x.getParentCode(), "0"))
                .map(x -> new MenuTreeDTO(x, menuList)).collect(Collectors.toList());

        List<MenuEnum> menuAllList = Arrays.stream(MenuEnum.values()).collect(Collectors.toList());
        setIsPermission(tree, menuList, menuAllList);
        return tree;
    }

    /**
     * 获取菜单树
     */
    private static void getMenuTree(List<MenuTreeDTO> list, List<MenuEnum> allMenuList) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        list.forEach(x -> {
            allMenuList.stream().filter(a -> Objects.equals(x.getMenuCode(), a.getParentCode())).forEach(y -> {
                if (x.getChildren() == null) {
                    List<MenuTreeDTO> children = new ArrayList<>();
                    children.add(new MenuTreeDTO(y));
                    x.setChildren(children);
                } else if (x.getChildren().stream().noneMatch(z -> Objects.equals(z.getMenuCode(), y.getCode()))) {
                    x.getChildren().add(new MenuTreeDTO(y));
                }
            });

            getMenuTree(x.getChildren(), allMenuList);
        });
    }

    /**
     * 设置权限
     */
    private static void setIsPermission(List<MenuTreeDTO> list, List<MenuEnum> permitMenuList, List<MenuEnum> allMenuList) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        list.forEach(x -> {
            allMenuList.stream().filter(a -> Objects.equals(x.getMenuCode(), a.getParentCode())).forEach(y -> {
                if (x.getChildren() == null) {
                    List<MenuTreeDTO> children = new ArrayList<>();
                    children.add(new MenuTreeDTO(y, permitMenuList));
                    x.setChildren(children);
                } else if (x.getChildren().stream().noneMatch(z -> Objects.equals(z.getMenuCode(), y.getCode()))) {
                    x.getChildren().add(new MenuTreeDTO(y, permitMenuList));
                }
            });

            setIsPermission(x.getChildren(), permitMenuList, allMenuList);
        });
    }
}